package com.zhuoshi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhuoshi.bean.ManagerBean;
import com.zhuoshi.bean.NavigationBean;
import com.zhuoshi.utils.DbHelper;

/**
 * 导航菜单表
 * @author Administrator
 *
 */
public class NavigationDao {
//	public List<NavigationBean> getDataList(int parent_id){
//		List<NavigationBean> list = new ArrayList<NavigationBean>();
//		boolean b= list.contains(new NavigationBean().getAction_type());
//		return list;
//	}
	
	
	private StringBuffer html = new  StringBuffer();
	private List<NavigationBean> nodes;
	
	public NavigationDao(){}
	
	public NavigationDao(List<NavigationBean> nodes){
		this.nodes = nodes;
	}
	
	public String buildTree(){
		html.append("<dl id=\"menu-setting\">");
		for (NavigationBean bean : nodes) {
			//Integer id = bean.getId();
			if(bean.getParent_id()==0){
				html.append("<dt class=\"\">\r\n<i class=\"Hui-iconfont "+bean.getSubtitle()+"\"></i>");
				html.append(bean.getTitle());
				html.append("\r\n<i class=\"Hui-iconfont menu_dropdown-arrow\"></i>\r\n</dt>");
                    
				//html.append("\r\n<li id='"+id+"'>"+bean.getTitle()+"</li>");
				build(bean);//加载子菜单
			}
		}
		html.append("\r\n</dl>");  
		
		return html.toString();
	}
	
	
	private void build(NavigationBean node){
		List<NavigationBean> children = getChildren(node);
		if(!children.isEmpty()){
			html.append("<dd style=\"display:none;\">");
			html.append("\r\n<ul>");
			for(NavigationBean child : children){
				//Integer id = child.getId();
				//html.append("\r\n<li id='"+id+"'>"+child.getTitle()+"("+child.getLinkurl()+")</li>");
				html.append("\r\n<li><a _href=\""+child.getLinkurl()+"\" href=\"javascript:void(0)\" data-title=\""+child.getTitle()+"\">"+child.getTitle()+"</a></li>");
				build(child);
			}
			html.append("\r\n</ul>");
			html.append("\r\n</dd>");
		}
	}
	
	private List<NavigationBean> getChildren(NavigationBean node){
		List<NavigationBean> children = new ArrayList<NavigationBean>();
		Integer id = node.getId();
		for (NavigationBean child : nodes) {
			if(id.equals(child.getParent_id())){
				children.add(child);
			}
		}
		return children;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 获取所有下级类别列表
	 * @param parend_id 父ID
	 */
	private void getChilds(List<NavigationBean> oldList,List<NavigationBean> newList,int parent_id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
//		List<NavigationBean> list = new ArrayList<NavigationBean>();
		//oldList = getDataList(parent_id);
		
			
		
		try {
			for(NavigationBean nBean : oldList){
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_navigation where parent_id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, parent_id);
			rs = pstat.executeQuery();
			while(rs.next()){
				NavigationBean bean = new NavigationBean();
				bean.setId(rs.getInt("id"));
				bean.setTitle(rs.getString("title"));
				bean.setSubtitle(rs.getString("subtitle"));
				bean.setLinkurl(rs.getString("linkurl"));
				bean.setSort_id(rs.getInt("sort_id"));
				bean.setEnabled(rs.getInt("enabled"));
				bean.setParent_id(rs.getInt("parent_id"));
				bean.setClass_list(rs.getString("class_list"));
				bean.setClass_layer(rs.getInt("class_layer"));
				bean.setAction_type(rs.getString("action_type"));
				bean.setSys(rs.getInt("sys"));
				newList.add(bean);
			}
				this.getChilds(oldList, newList, nBean.getParent_id());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		
		//return list;
	}
	/**
	 * 取得所有类别列表
	 * @param id  
	 * @return
	 */
	public List<NavigationBean> getList(int parent_id,ManagerBean managerBean,int role_id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<NavigationBean> oldList = new ArrayList<NavigationBean>();
		//List<NavigationBean> newList = new ArrayList<NavigationBean>();
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			if(managerBean.getRole_type()>1){
				sql.append("select * from mt_navigation n,mt_manager_role_value r where n.id=r.nav_id and r.role_id="+role_id+" and r.action_type='SHOW'");
			}else{
				sql.append("select * from mt_navigation where 1=1");
			}
			pstat = conn.prepareStatement(sql.toString());
			//pstat.setInt(1, parent_id);
			rs = pstat.executeQuery();
			while(rs.next()){
				NavigationBean bean = new NavigationBean();
				bean.setId(rs.getInt("id"));
				bean.setTitle(rs.getString("title"));
				bean.setSubtitle(rs.getString("subtitle"));
				bean.setLinkurl(rs.getString("linkurl"));
				bean.setSort_id(rs.getInt("sort_id"));
				bean.setEnabled(rs.getInt("enabled"));
				bean.setParent_id(rs.getInt("parent_id"));
				bean.setClass_list(rs.getString("class_list"));
				bean.setClass_layer(rs.getInt("class_layer"));
				bean.setAction_type(rs.getString("action_type"));
				bean.setSys(rs.getInt("sys"));
				oldList.add(bean);
			}
			
//			newList = oldList;
//			getChilds(oldList, newList, parent_id);
			//return newList;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return oldList;
		
	}
	/**
	 * 获取所有类别数据(没有排序好，只有数据)
	 * @param parent_id 父ID 0为所有类别
	 * @return
	 */
	public List<NavigationBean> getDataList(int parent_id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<NavigationBean> list = new ArrayList<NavigationBean>();
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_navigation where 1=1");
//			sql.append(" and class_layer=1");
			sql.append(" order by sort_id asc,id asc");
			pstat = conn.prepareStatement(sql.toString());
//			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while(rs.next()){
				NavigationBean bean = new NavigationBean();
				bean.setId(rs.getInt("id"));
				bean.setTitle(rs.getString("title"));
				bean.setSubtitle(rs.getString("subtitle"));
				bean.setLinkurl(rs.getString("linkurl"));
				bean.setSort_id(rs.getInt("sort_id"));
				bean.setEnabled(rs.getInt("enabled"));
				bean.setParent_id(rs.getInt("parent_id"));
				bean.setClass_list(rs.getString("class_list"));
				bean.setClass_layer(rs.getInt("class_layer"));
				bean.setAction_type(rs.getString("action_type"));
				bean.setSys(rs.getInt("sys"));
				list.add(bean);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		
		return list;
	}
	
	public NavigationBean getModel(int id){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		NavigationBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from mt_navigation where 1=1");
			sql.append(" and id=?");
//			sql.append(" and parent_id=0");
			sql.append(" order by sort_id asc,id asc");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			if(rs.next()){
				bean = new NavigationBean();
				bean.setId(rs.getInt("id"));
				bean.setTitle(rs.getString("title"));
				bean.setSubtitle(rs.getString("subtitle"));
				bean.setLinkurl(rs.getString("linkurl"));
				bean.setSort_id(rs.getInt("sort_id"));
				bean.setEnabled(rs.getInt("enabled"));
				bean.setParent_id(rs.getInt("parent_id"));
				bean.setClass_list(rs.getString("class_list"));
				bean.setClass_layer(rs.getInt("class_layer"));
				bean.setAction_type(rs.getString("action_type"));
				bean.setSys(rs.getInt("sys"));
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		
		return bean;
	}
	
}
